"""筛选出并发症没有title的"""
import os
import json

path = r'D:\PycharmProjects\39html_data\json_data'
res_path = r'D:\PycharmProjects\39html_data\json_data\comp'
# print(os.listdir(path))
# result_list = []
# for file in os.listdir(path):
#
#     file_path = os.path.join(path, file)
#     if os.path.isfile(file_path):
#         with open(file_path, mode='r', encoding='utf-8') as f:
#             title_list = []
#             json_data = json.load(f)
#             # print(json_data['title'])
#             for item in json_data['content']:
#                 title_list.append(item.get('title'))
#             comp_title = json_data['title'] + '并发症'
#             if comp_title not in title_list and 'title' not in json_data['content'][-1]:
#                 result_list.append(file)
# print(result_list)
# print(len(result_list))

# 融合 text 构造并发症
result_list = ['dny.json', 'fm.json', 'fw.json', 'gdtqzhz.json', 'gm1.json', 'go.json', 'hhb.json', 'hnxsxy.json',
               'jb3.json', 'jfywjztyhrgjztydqgy.json', 'jqy.json', 'jxdgy.json', 'lnrbdxgy.json', 'lnrjxlwy.json',
               'lxjx.json', 'mdeycb.json', 'qcb.json', 'qwnzd.json', 'rshbzzjwl.json', 'rx1.json', 'rxxb.json',
               'stky.json', 'swmdmzs.json', 'thb.json', 'tj.json', 'tnbpfgr.json', 'xx3.json', 'ydfscdzz.json',
               'ydl.json', 'yszcdzz.json', 'ywzdxzwsjb.json', 'yy2.json', 'yzjptcz.json']

for file in result_list:
    file_path = os.path.join(path, file)
    with open(file_path, mode='r', encoding='utf-8') as f:
        json_data = json.load(f)
        disease_name = json_data['title']
        content = json_data['content']

        comp_dict = {'title': disease_name + '并发症', 'content': []}
        i = len(content) - 1
        while True:
            if content[i].get('title'):
                break
            item = content.pop(i)
            comp_dict['content'].insert(0, item)
            i = i - 1
        content.append(comp_dict)
        # print(comp_dict)
        with open(os.path.join(res_path, file), mode='w', encoding='utf-8') as f1:
            json.dump(json_data, f1, ensure_ascii=False)
